const app = getApp();
var CryptoJS = require('/utils/crypto-js.js');

const properties={
  // apiUrl: 'https://55005i9j44.goho.co',//本地环境
  // apiUrl:'https://crouter.yunzongnet.com/zdsd-api/t6870',//测试环境
  apiUrl:'https://apizhengdian.ruohuo.net/api',//正式环境
  dhUrl:'https://money.v0719.com',

};

/*
该项目接口
*/
const apiAddress={
  userInfo:'/user/baseInfo', //获取用户基本信息
  getToken:'/user/token', //获取token
  login:'/user/save', //保存-修改用户信息
  advert:'/advert/list', //获取广告列表
  coinRecord:'/incomeExpense/record/coin',//金币记录
  redPacket:'/incomeExpense/record/redPacket', //红包记录
  exchange:'/incomeExpense/exchange', //金币兑换
  withdraw:'/incomeExpense/withdraw',//红包提现 
  enable:'/active/enable',//是否可以领取奖励
  claim:'/active/claim', //领取奖励
  account:'/user/account', //获取用户账户信息
  signList:'/signIn/list', //获取签到日期列表
  submitSign:'/signIn/submit', //提交签到
  task:'/advert/task/list', //任务流广告
  checkAdv:'/advert/ym/finishStatus/query', //查询云码广告完成状态
  getSwiper:'/advert/slideshow/list', //轮播图
  startBrowse:'/advert/rh/begin', //开始浏览ruohuo
  endBrowse:'/advert/rh/end', //结束浏览ruohuo
  startDh:'/advert/dh/begin',//开始浏览denghuo
  endDh:'/advert/dh/end', //结束浏览denghuo
  queryDh:'/advert/dh/progress/query', //查询灯火底部广告完成进度
  queryBanner:'/advert/dh/reward/query', //查询banner位奖励
  querySwtich:'/advert/switch/query',//查询广告开关设置
  invitaReward:'/invite/viewAward',//查看邀请奖励
  inviteResult:'/invite/viewResult',//查看邀请结果
  confirmInvite:'/invite/confirmResult',//确认邀请结果

  dhYindao:'/ad-white-list/get-dh-by-uid'
}
function aesEncrypt(encryptString, key, iv) {
  var key = CryptoJS.enc.Utf8.parse(key);
  var srcs = CryptoJS.enc.Utf8.parse(encryptString);
  var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: CryptoJS.enc.Utf8.parse(iv), mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
  return encrypted.toString();
}

function encryRequest(api, data ,headerType ,methods){
  let accessToken = app.globalData.token;	
  let key = "wzJ1lZThZoZg4i97",iv = "2b6SFVfhEmHyqtJg";
  let time = Date.parse(new Date());
  let pazzword = aesEncrypt(time,key,iv);
  let dataheader = {
    'content-type': (headerType == '' || headerType==undefined)? 'application/json' : 'application/x-www-form-urlencoded',
     'TOKEN':accessToken || '',
     'R_TOKEN':pazzword,
  };
  return new Promise((resolve, reject) => {
      my.showLoading({
        title: '加载中...',
      });
    my.request({
      url: properties.apiUrl + api,
      data: data,
      headers:dataheader,
      method: methods,
      success: function (res) {
        resolve(res.data);
        my.hideLoading();
      },
      fail: function (res) {
        reject(res);
        my.hideLoading();
      }
    })
  });
}

function publicRequest(api, data ,headerType ,methods){
  let accessToken = app.globalData.token;
  // my.gets
  let dataheader = {
    'content-type': (headerType == '' || headerType==undefined)? 'application/json' : 'application/x-www-form-urlencoded',
     'TOKEN':accessToken || '',
  };
  return new Promise((resolve, reject) => {
      my.showLoading({
        title: '加载中...',
      });
    my.request({
      url: properties.apiUrl + api,
      data: data,
      headers:dataheader,
      method: methods,
      success: function (res) {
        resolve(res.data);
        my.hideLoading();
      },
      fail: function (res) {
        reject(res);
        my.hideLoading();
      }
    })
  });
}

function dhRequest(api , data){
  let header = {
    'content-type':'application/json',
    'dataType':"JSON",
  };
  return new Promise((resolve,reject) =>{
    my.showLoading({
      title: '加载中...',
    });
    my.request({
      url:properties.dhUrl + api,
      data: data,
      headers:header,
      method: 'post',
      success: function (res) {
        resolve(res.data);
        my.hideLoading();
      },
      fail: function (res) {
        reject(res);
        my.hideLoading();
      }
    })
  })
}

module.exports = {
  apiAddress:apiAddress,
  publicRequest:publicRequest,
  encryRequest:encryRequest,
  dhRequest:dhRequest,
}